<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="600" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" :disabled="mode=='show'" ref="dialogForm" label-width="100px"
			label-position="left">
			<!-- <el-form-item label="头像" prop="avatar">
				<sc-upload v-model="form.avatar" title="上传头像"></sc-upload>
			</el-form-item> -->
			<el-form-item label="登录账号" prop="account">
				<el-input v-model="form.account" placeholder="用于登录系统" clearable></el-input>
			</el-form-item>
			<el-form-item label="姓名" prop="name">
				<el-input v-model="form.name" placeholder="请输入完整的真实姓名" clearable></el-input>
			</el-form-item>
			<el-form-item label="登录密码" :prop="form.id ? '':'password'">
				<el-input type="password" v-model="form.password" clearable show-password
					placeholder="密码不修改,请留空"></el-input>
			</el-form-item>
			<el-form-item label="联系方式" prop="mobilePhone">
				<el-input v-model="form.mobilePhone" placeholder="请输入联系方式" clearable></el-input>
			</el-form-item>
			<el-form-item label="所属单位" prop="unit">
				<Unit v-model="form.unit" placeholder="请选择所属单位"></Unit>
			</el-form-item>
			<el-form-item label="所属部门" prop="">
				<el-cascader v-model="form.dept" :options="groups" :props="groupsProps" :show-all-levels="false"
					clearable placeholder="请选择所属部门"></el-cascader>
			</el-form-item>
			<el-form-item label="所属门店" prop="stores">
				<Store v-model="form.stores" :unit="unit" multiple placeholder="请选择所属门店"></Store>
			</el-form-item>
			<el-form-item label="所属角色" prop="">
				<!-- <el-select v-model="form.roles" multiple filterable style="width: 100%">
					<el-option v-for="item in rolesList" :key="item.id" :label="item.name" :value="item.id" />
				</el-select> -->
				<sc-select v-model="form.roles" :apiObj="$API.system.role.list" :props="rolesProps" :params="rolesParams" clearable filterable
					multiple style="width: 100%;"></sc-select>
			</el-form-item>
			<el-form-item v-if="userInfo.system" label="系统管理员" prop="action">
				<el-switch v-model="form.action"></el-switch>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible=false">取 消</el-button>
			<el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
</template>

<script>
	export default {
		emits: ['success', 'closed'],
		data() {
			return {
				mode: "add",
				titleMap: {
					add: '新增用户',
					edit: '编辑用户',
					show: '查看'
				},
				visible: false,
				isSaveing: false,
				//所需数据选项
				groups: [],
				groupsProps: {
					value: "id",
					label: 'name',
					emitPath: false,
					checkStrictly: true
				},
				rolesParams: {
					page: 1,
					pagesize: 100
				},
				rolesProps: {
					label: 'name',
					value: 'id'
				},
				//表单数据
				form: {
					name: null,
					unit: {
						id: null
					},
					stores: [],
					action: false,
					roles: [],
					dept: null
				},
				params: {
					page: 1,
					pagesize: 20
				},
				unit: null,
				//验证规则
				rules: {
					account: [{
						required: true,
						message: '请输入登录账号'
					}],
					name: [{
						required: true,
						message: '请输入真实姓名'
					}],
					password: [{
							required: true,
							message: '请输入登录密码'
						},
						{
							validator: (rule, value, callback) => {
								if (this.form.password2 !== '') {
									this.$refs.dialogForm.validateField('password2');
								}
								callback();
							}
						}
					],
					password2: [{
							required: true,
							message: '请再次输入密码'
						},
						{
							validator: (rule, value, callback) => {
								if (value !== this.form.password) {
									callback(new Error('两次输入密码不一致!'));
								} else {
									callback();
								}
							}
						}
					],
					dept: [{
						required: true,
						message: '请选择所属部门'
					}],
					roles: [{
						required: true,
						message: '请选择所属角色',
						trigger: 'change'
					}]
				},
				depts: [],
				deptsProps: {
					value: "id",
					checkStrictly: true
				},
				rolesList: [],
				userInfo: {}
			}
		},
		watch: {
			'form.unit'() {
				this.unit = this.form.unit.id
			}
		},
		created() {
			this.userInfo = this.$TOOL.data.get("USER_INFO")
			console.log(this.userInfo)
			this.getGroup()
		},
		mounted() {

			//this.getRolesList()
		},
		methods: {
			//显示
			open(mode = 'add') {
				this.mode = mode;
				this.visible = true;
				return this
			},
			// 加载部门
			async getGroup() {
				var res = await this.$API.system.dept.list.post();
				this.groups = res.data;
			},
			async getRolesList() {
				var res = await this.$API.system.role.list.post(this.rolesParams);
				console.log(res)
				this.rolesList = res.data.datas;
			},
			//表单提交方法
			submit() {
				this.$refs.dialogForm.validate(async (valid) => {
					if (valid) {
						this.isSaveing = true;
						let stores = []
						if (this.form.stores.length) {
							this.form.stores.forEach(item => {
								stores.push({
									id: item.id
								})
							})
						} else {
							stores = null
						}

						let roles = []
						if (this.form.roles.length) {
							this.form.roles.forEach(item => {
								roles.push({
									id: item
								})
							})
						} else {
							roles = null
						}
						let data = {
							account: this.form.account,
							name: this.form.name,
							password: this.form.password,
							mobilePhone: this.form.mobilePhone,
							unit: {
								id: this.form.unit.id
							},
							stores: stores,
							dept: {
								id: this.form.dept
							},
							roles: roles,
							action: this.form.action
						}
						if (this.form.id) {
							data.id = this.form.id
						}
						var res = await this.$API.system.user.save.post(data);
						this.isSaveing = false;
						if (res.code == 200) {
							this.$emit('success', this.form, this.mode)
							this.visible = false;
							this.$message.success("操作成功")
						} else {
							this.$alert(res.message, "提示", {
								type: 'error'
							})
						}
					} else {
						return false;
					}
				})
			},
			//表单注入数据
			setData(data) {
				//可以和上面一样单个注入，也可以像下面一样直接合并进去
				Object.assign(this.form, data)
				this.form.dept = data.deptId
				this.form.unit.id = data.unitId
				this.form.unit.name = data.unitName
				if (this.form.roles.length) {
					let _arr = []
					this.form.roles.forEach(item => {
						_arr.push(item.id)
					})
					this.form.roles = _arr
				}
			}
		}
	}
</script>

<style>
</style>
